/**
  * Author: Mei
  * Date: 2020-10-14 20:29
  * Description: 
  *
  */
#include <iostream>

using namespace std;

/**
 * 运算符重载
 * @return
 */
class Complex {
private:
    double real;
    double image;
public:
    Complex() {
        real = 0;
        image = 0;
    }

    int getReal() const {
        return real;
    }

    int getImage() const {
        return image;
    }

    Complex(double x, double y) : real(x), image(y) {};

    Complex operator+(const Complex &c);

    friend Complex & operator+=(const Complex &c);

};

Complex Complex::operator+(const Complex &c) { //成员函数的写法
    Complex cc;
    cc.real = this->real + c.real;
    cc.image = this->image + c.image;
    return cc; //cc释放之前会调用默认复制构造函数  浅复制
}

friend Complex & Complex::operator+=(const Complex &c, const Complex &c1) { //c1 += c2 ->c1 = c1 + c2
    //this->real += c.real;
    //this->image += c.image;
    //
    //return *this;
    Complex d = Complex(c.real, c.image);
    return d;
}

Complex operator-(Complex &c1, Complex &c2) {
    return Complex(c1.getReal() - c2.getReal(), c1.getImage() - c2.getImage());
}

int main() {
    Complex c(1, 2);
    Complex cc(3, 5);
    cout << (c + cc).getReal() << ", " << (c + cc).getImage() << endl; //本质上还是函数调用
    cout << (c - cc).getReal() << ", " << (c - cc).getImage() << endl; //本质上还是函数调用

    //友元函数的运算符重载必须要有两个参数
    //一般单目运算符为成员函数 !, ++, --，双目运算符重载为友元函数 +, -, ==, &&
    c += cc;
    cout << (c).getReal() << ", " << (c).getImage() << endl; //本质上还是函数调用

    return 0;
}